//
//  CommentService.m
//  TipsProject
//
//  Created by Suny on 15/6/18.
//  Copyright (c) 2015年 Suny. All rights reserved.
//

#import "CommentService.h"
#import "TipCommentDisplayInfo.h"

@implementation CommentService

- (void)publishComment:(TipComment *)comment finishBlock:(RequestFinishBlock)finishBlock
{
    NSDictionary *dictionary = [comment dictionaryWithNameAndValue];

    WEAKSELF;
    AFHTTPRequestOperation * requestOperation = [self beginRequestWithRelativePath:kCommentUrl
                                                                      parameterObject:dictionary
                                                                        responseClass:nil
                                                                          finishBlock:^(BaseResponse *response) {
                                                        
                                                                              finishBlock(response);

                                                                          }];

}

- (void)getCommentListByTipId:(NSString *)tipId pageNumber:(NSInteger)page finishBlock:(RequestFinishBlock)finishBlock
{

	NSString *relativePath = [NSString stringWithFormat:@"%@/%@",kCommentUrl,tipId];
	NSDictionary *dictionary = @{@"curPage":[NSString stringWithFormat:@"%ld",(long)page],@"pageSize":@kDefaultPageSize};
	WEAKSELF;
	AFHTTPRequestOperation * requestOperation = [self beginGetRequestWithRelativePath:relativePath
																	  parameterObject:dictionary
																		responseClass:nil
																		  finishBlock:^(BaseResponse *response) {
																			  [weakSelf removeWrapperForKey:relativePath];
																			  
																			  if (response.success)
																			  {
																				  NSMutableArray *resultArray = [NSMutableArray array];
																				  NSArray *temArray = [response.object objectForKey:@"pageContent"];
																				  
																				  for (NSDictionary *dic in temArray)
																				  {
																					  TipComment *comment = [[TipComment alloc] initWithSourceString:dic];

                                                                                      TipCommentDisplayInfo *displayInfo = [[TipCommentDisplayInfo alloc] initWithModelInfo:comment];
                                                                                      [resultArray addObject:displayInfo];
																				  }
																				  response.object = resultArray;
																			  }
																			  
																			  finishBlock(response);
																			  
																		  }];
	[self setWrapper:requestOperation ForKey:relativePath];
}

- (void)deleteCommentByCommentId:(NSString *)commentId andUserId:(NSString *)userId finishBlock:(RequestFinishBlock)finishBlock
{
	
	NSDictionary *dictionary = @{@"memberId":userId,@"commentId":commentId};
	
	WEAKSELF;
	AFHTTPRequestOperation * requestOperation = [self beginGetRequestWithRelativePath:kCommentDeleteUrl
																	  parameterObject:dictionary
																		responseClass:nil
																		  finishBlock:^(BaseResponse *response) {
																			  
																			  finishBlock(response);
																			  
																		  }];
}

@end
